<!-- eslint-disable vue/require-default-prop -->
<!-- eslint-disable vue/require-default-prop -->
<!-- eslint-disable vue/require-prop-types -->
<template>
  <el-dialog title="收货地址" :visible="isVisible" @close="close">
    <div class="form-container">
      <el-form ref="addForm" :model="rentForm" :rules="rentRules" label-position="top">
        <el-form-item label="租赁楼宇" prop="buildingId">
          <el-select v-model="rentForm.buildingId" placeholder="请选择">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租赁起止日期" prop="rentTime">
          <el-date-picker
            v-model="rentForm.rentTime"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="租赁合同" prop="contractId">
          <el-upload
            action="#"
            :http-request="upload"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-button size="small" type="primary" plain>上传合同文件</el-button>
            <div slot="tip" class="el-upload__tip">支持扩展名：.doc .docx .pdf, 文件大小不超过2M</div>
          </el-upload>
        </el-form-item>
        <el-button type="primary" @click="addSubmit">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form>
    </div>
  </el-dialog>

</template>

<script>
import { getRentBuildListAPI, addEnterpriseContractAPI, uploadAPI } from '@/api/enterprise'
export default {
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    // eslint-disable-next-line vue/require-default-prop
    dataId: {
      type: Number
    },
    // eslint-disable-next-line vue/require-default-prop
    getEnterpriseList: {
      type: Function
    }
  },
  data() {
    return {
      // 获取楼宇列表
      list: [],
      // 表单数据
      rentForm: {
        buildingId: null, // 楼宇id
        contractId: null, // 合同id
        contractUrl: '', // 合同Url
        type: 0, // 合同类型
        rentTime: [], // 合同时间
        enterpriseId: null // 企业id
      },
      // 表单验证规则
      rentRules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        rentTime: [
          { required: true, message: '请选择租赁日期', trigger: 'change' }
        ],
        contractId: [
          { required: true, message: '请上传合同文件', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    // 获取楼宇列表，渲染
    this.getRentBuildList()
  },
  methods: {
    // 获取楼宇列表，封装接口
    async getRentBuildList() {
      const res = await getRentBuildListAPI()
      this.list = res.data
      this.rentForm.buildingId = res.data.id
    },
    // 提交表单
    async addSubmit() {
      // 把父级传过来的id赋值给企业id
      this.rentForm.enterpriseId = this.dataId
      // 解构
      const { buildingId, contractUrl, contractId, type, enterpriseId } = this.rentForm
      // 调用接口
      await addEnterpriseContractAPI({ buildingId, contractUrl, contractId, type, enterpriseId, startTime: this.rentForm.rentTime[0], endTime: this.rentForm.rentTime[1] })
      // 关闭弹窗,从父级获取关闭事件
      this.$emit('update:isVisible', false)
      // 从父级调用获取企业列表方法
      this.getEnterpriseList()
      // 弹框显示
      this.$message.success('添加成功')
      // console.log(this.rentForm, res)
    },
    // 取消
    cancel() {
      // 子组件向父组件传递关闭事件
      this.$emit('update:isVisible', false)
      // 取消表单，并重置
      this.$refs.addForm.resetFields()
    },
    // 关闭
    close() {
      // 子组件向父组件传递关闭事件
      this.$emit('update:isVisible', false)
      // 取消表单，并重置
      this.$refs.addForm.resetFields()
    },
    // 上传文件
    async upload(data) {
      const fd = new FormData()
      fd.append('file', data.file)
      fd.append('type', 'contract')
      const res = await uploadAPI(fd)
      this.rentForm.contractId = res.data.id
      this.rentForm.contractUrl = res.data.url
    },
    // 上传处理
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    // 上传前验证
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }

  }
}
</script>

<style>

</style>
